<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      background: #e0e0e0;
    }

    .container {
      width: 240px;
      margin: 200px auto;
      user-select: none;
      padding: 10px;
      border-radius: 10px;
      box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.5);
    }

    button {
      width: 50px;
      height: 50px;
      margin: 3px;
      border: none;
      border-radius: 5px;
      background: #e0e0e0;
      box-shadow: 5px 5px 9px #afafaf,
        -5px -5px 9px #ffffff;
      font-size: 18px;
    }

    button:active {
      background: linear-gradient(145deg, #cacaca, #f0f0f0);
      box-shadow: 5px 5px 7px #bebebe,
        -5px -5px 7px #ffffff;
    }

    .btn2 {
      width: 110px;
    }

    .dengyu {
      background: #2d8cf0;
      color: #ffffff;
      /* box-shadow: 5px 5px 10px #2981dd,
        -5px -5px 10px #3197ff; */
    }

    .dengyu:active {
      background: #2b85e4;
    }

    .show {
      height: 60px;
      border-radius: 6px;
      background: #e0e0e0;
      box-shadow: inset 5px 5px 9px #afafaf,
        inset -5px -5px 9px #ffffff;
      margin-bottom: 10px;
    }

    #result {
      padding: 10px;
      font-size: 20px;
      font-family: Verdana, Geneva, Tahoma, sans-serif;
      line-height: 60px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="show">
      <div id="result"></div>
    </div>
    <div>
      <button onclick="getValue('1')">1</button>
      <button onclick="getValue('2')">2</button>
      <button onclick="getValue('3')">3</button>
      <button onclick="getValue('+')">+</button>
    </div>
    <div>
      <button onclick="getValue('4')">4</button>
      <button onclick="getValue('5')">5</button>
      <button onclick="getValue('6')">6</button>
      <button onclick="getValue('-')">-</button>
    </div>
    <div>
      <button onclick="getValue('7')">7</button>
      <button onclick="getValue('8')">8</button>
      <button onclick="getValue('9')">9</button>
      <button onclick="getValue('*')">×</button>
    </div>
    <div>
      <button onclick="getValue('.')">.</button>
      <button onclick="getValue('0')">0</button>
      <button onclick="del()">Del</button>
      <button onclick="getValue('/')">÷</button>
    </div>
    <div>
      <button class="btn2" onclick="clearResult()">C</button>
      <button class="btn2 dengyu" onclick="calculate(result)">=</button>
    </div>
  </div>
  <script>

    const res = document.getElementById('result')
    let result = null

    // 获取值
    function getValue(enteredValue) {
      if (!result) {
        result = "";
      }
      result += enteredValue;
      res.innerText = result
      res.style.textAlign = 'left'
    }

    // 计算
    function calculate(value) {
      const calculatedValue = eval(value || null);
      if (isNaN(calculatedValue)) {
        result = "Can't divide 0 with 0";
        setTimeout(() => {
          result = "";
        }, 1300);
      } else {
        result = calculatedValue;
      }
      res.innerText = result
      res.style.textAlign = 'right'
    }

    // 清除
    function clearResult() {
      result = ""
      res.innerText = result
    }

    // 删除字符
    function del() {
      result = result.slice(0, -1)
      res.innerText = result
    }
  </script>
</body>

</html>